$(function(){
	var linkListDiv = $('#link-list-div');
		modal = $('#link-modal'), //模态框
		ListMenu = { //列表菜单
			'新建': function(){
				modal.one('shown.bs.modal', function(){
					$('#link_title').focus();
				}).modal('show').find('form').attr('action', SITE_URL+'mod.php?link::add').find('input[name="link_id"]').remove();
				$('#link_title').val('');
				$('#link_url').val('');
				$('#link_desc').val('');
				$('#link_logo').val('');
				$('#link-logo').attr('src', '');
			},
			'编辑': function(){
				var target = linkListDiv.find('.active').eq(0),
					id = target.data('id'),
					mediaBody = target.find('.media-body'),
					linkTitle = mediaBody.children('h4').text().replace(/\s*/g, ''),
					linkURL = mediaBody.children('p').eq(0).text().replace(/\s*/g, ''),
					linkDesc = mediaBody.children('p').eq(1).text().replace(/\s*/g, ''),
					linkLOGO = target.find('.media-left img').attr('src');
				modal.one('shown.bs.modal', function(){
					$('#link_title').focus();
				}).modal('show').find('form').attr('action', SITE_URL+'mod.php?link::update').prepend('<input type="hidden" name="link_id" value="'+id+'">');
				$('#link_title').val(linkTitle);
				$('#link_url').val(linkURL);
				$('#link_desc').val(linkDesc);
				$('#link_logo').val(linkLOGO);
				$('#link-logo').attr('src', linkLOGO).attr('data-origin', linkLOGO);
			},
			'删除': function(){
				linkListDiv.editMulti({
					obj: 'link',
					act: 'delete',
					unit: '条',
					action: '删除',
					item: '链接'
				});
			}
		},
		showListMenu = function(){ //显示菜单
			var length = linkListDiv.find('.active').length;
			if(!length){
				var menu = {'新建': ListMenu['新建']};
			}else{
				if(length == 1){
					var menu = {'编辑': ListMenu['编辑'], '删除': ListMenu['删除']};
				}else{
					var menu = {'删除': ListMenu['删除']};
				}
			}
			$.showMenu(menu);
		};
	showListMenu();

	/** 点击列表项时对其进行高亮 */
	linkListDiv.on('click', '.media', function(){
		$(this).toggleClass('active');
		showListMenu();
	});

	/** 提交编辑链接的表单 */
	$('#link-modal form').ajaxSubmit(function(result){
		if(result.success){
			if($(this).attr('action') == SITE_URL+'mod.php?link::update'){
				var active = linkListDiv.find('.active').eq(0);
				var target = active.find('.media-body');
				target.children('h4').text(result.data.link_title);
				target.children('p').eq(0).text(result.data.link_url);
				target.children('p').eq(1).text(result.data.link_desc);
				active.find('.media-left .media-logo').html('<img src="'+result.data.link_logo+'" alt="链接 LOGO"/>');
				$('#link-modal').modal('hide');
			}else{
				alert('链接添加成功。');
				location.reload();
			}
		}else{
			alert(result.data);
		}
	});

	/** 选择链接 LOGO 后将其上传到服务器 */
	$('#select-logo').click(function(){
		$this = $(this);
		$this.button('loading');
		$.upload({
			url: SITE_URL+'mod.php?file::upload',
			success: function(result){
				$this.button('reset');
				if(result.success){
					$('#link_logo').val(result.data[0].file_src);
					$('#link-logo').attr('src', result.data[0].file_src);
					$this.button('reset').text('更换');
				}else{
					if(typeof result.data == 'string'){
						alert(result.data);
					}else{
						alert(result.data[0].error);
					}
				}
			},
			error: function(xhr){
				alert('与服务器的连接出现错误，文件上传失败。');
				console.log(xhr.responseText);
				$this.button('reset');
			}
		});
	});

	/** 显示帮助信息 */
	$.showHelp('和其他网站交换友情链接有助于提高站点被其他网络用户发现的几率，从而增加网络流量。')
})